package com.infonautics.action;

import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;

import com.hibernate.Company;
import com.hibernate.Userinfo;
import com.infonautics.dao.ICompanyDao;
import com.infonautics.dao.imp.ICompanyDaoImp;
import com.infonautics.page.PageBean;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.system.dao.ISystemDao;

public class CompanyAction extends ActionSupport implements Preparable,SessionAware{
	private static final long serialVersionUID = 1L;
	private Map<String,Object> session;
	private String pageUrl = "/WEB-INF/company/";
	private String successMsg;
	private String url;
	private String errormessage;
	private Userinfo userinfo;
	private ISystemDao<Company> companyDao = new ICompanyDaoImp();
	private ICompanyDao comDao = new  ICompanyDaoImp();
	private Company company;
	private PageBean<Company> pageBean;
	private int page = 1;
	private int id;
	private String companyname;
	@Override
	public void prepare() throws Exception {
		if(session.containsKey("userinfo")){
			if(session.get("userinfo")!=null){
				userinfo=(Userinfo)session.get("userinfo");
			}
		}
		
	}
	
	//查询分公司列表
	public String list() throws Exception{
		pageBean = comDao.doCompanyList(20, page);
		return "input";
	}

	public String query() throws Exception{
		String queryString = "";
		if(companyname !=null && !companyname.equals("")){
			companyname=new String(companyname.getBytes("ISO8859-1"),"UTF-8");
			queryString += "and name like '%" + companyname + "%'";
		}
		pageBean = comDao.doQueryList(20, page, queryString);
		pageBean.setQueryString(queryString);
		return "list";
	}
	//新增分公司页面
	public String addPage()throws Exception{
		return "input";
	}
	
	//新增分公司
	public String add() throws Exception{
		companyDao.doAdd(company);
		doMsgHandle("添加成功","Company_list.action");
		return CompanyAction.SUCCESS;
	}
	
	//修改分公司页面
	public String modifyPage() throws Exception{
		company = companyDao.doOne(id);
		return "input";
	}
	//修改分公司信息
	public String modify() throws Exception{
		companyDao.doModfiy(company);
		doMsgHandle("修改成功","Company_list.action");
		return CompanyAction.SUCCESS;
		
	}
	//删除分公司
	public String delete() throws Exception{
		companyDao.doDelete(id);
		doMsgHandle("删除成功","Company_list.action");
		return CompanyAction.SUCCESS;
	}
	//服务方法
	private void doMsgHandle(String successMsg,String url){
		this.successMsg=successMsg;
		this.url=url;
	}
	
	public Map<String, Object> getSession() {
		return session;
	}
	
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	public String getErrormessage() {
		return errormessage;
	}
	
	public void setErrormessage(String errormessage) {
		this.errormessage = errormessage;
	}
	
	public String getPageUrl() {
		return pageUrl;
	}

	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}

	public Userinfo getUserinfo() {
		return userinfo;
	}
	
	public void setUserinfo(Userinfo userinfo) {
		this.userinfo = userinfo;
	}
	
	public String getSuccessMsg() {
		return successMsg;
	}
	
	public void setSuccessMsg(String successMsg) {
		this.successMsg = successMsg;
	}
	
	public String getUrl() {
		return url;
	}
	
	public void setUrl(String url) {
		this.url = url;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public PageBean<Company> getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean<Company> pageBean) {
		this.pageBean = pageBean;
	}

	public String getCompanyname() {
		return companyname;
	}

	public void setCompanyname(String companyname) {
		this.companyname = companyname;
	}
	
	
}
